﻿using MyabpDDD.Demo.Application.yingSHow.Dto;
using MyCompany.MyProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyabpDDD.Demo.Application.yingSHow
{
    public class YinshowService : ApplicationService, IYinshowService
    {
        private readonly IRepository<Chargeablemanagement> _repository;
        private readonly IRepository<HeDui> _HeDui;
        private readonly IRepository<JieSuanTable> _JieSuanTable;
        public YinshowService(IRepository<Chargeablemanagement> students, IRepository<HeDui> heDuis, IRepository<JieSuanTable> jieSuanTables)
        {
            this._repository = students;
            this._HeDui = heDuis;
            this._JieSuanTable = jieSuanTables;
        }
        public dynamic QueryStudentAll(int pageIndex, int pageSize, string huozhudanwei, int? zhuangtai, string jinbanren, DateTime? date, int? jiesuan)
        {
            var q1 = _repository.AsQueryable();
            var q2 = _HeDui.AsQueryable();
            var q3= _JieSuanTable.AsQueryable();
            var query = from a in q1
                        join a2 in q2 on a.Sistrte equals a2.Id
                        join a3 in q3 on a.FangShi equals a3.Id
                        select new dfygrDto()
                        {
                            Id = a.Id,
                            NumBer = a.NumBer,
                            Danwei = a.Danwei,
                            FangShi = a.FangShi,
                            DunWei = a.Danwei,
                            Price = a.Price,
                            Sum = a.Sum,
                            YeWu = a.YeWu,
                            Ren = a.Ren,
                            CreateDate = a.CreateDate,
                            BeiZhu = a.BeiZhu,
                            Sistrte = a.Sistrte,
                            HeDui = a.HeDui,
                            HeDuiDate = a.HeDuiDate,
                            Name = a3.Name,
                        };
            if (zhuangtai>0 && zhuangtai!=null)
            {
                query = query.Where(x => x.Sistrte == zhuangtai);
            }
            if (!string.IsNullOrEmpty(huozhudanwei))
            {
                query=query.Where(x=>x.Danwei== huozhudanwei);
            }
            if (!string.IsNullOrEmpty(jinbanren))
            {
                query=query.Where(x=>x.Ren== jinbanren);
            }
            if (date != null)
            {
                query=query.Where(x=>x.YeWu == date);
            }
            if (jiesuan>0 && jiesuan!=null)
            {
                query = query.Where(x => x.FangShi == jiesuan);
            }
            var list = new
            {
                Zongtiao = query.Count(),
                data = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()
            };
            return list;
        }

        public bool SelectDel(string ids)
        {
            string[] int_ids = ids.Split(',');
            var q = from id in int_ids
                    select Convert.ToInt32(id);
            var q_list = q.ToList();
            var query = _repository.AsQueryable();
            query = query.Where(d => q_list.Contains
              (d.Id));
            foreach (var model in query)
            {
                _repository.DeleteAsync(model);
            }
            return true;
        }
    }
}
